//
//  MHTabBarAdd.swift
//  MHSinaWeiBo
//
//  Created by a on 15/9/30.
//  Copyright © 2015年 icast. All rights reserved.
//

import UIKit

class MHTabBar: UITabBar {

    //布局子控件时只需
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算btn的frame
        let w = self.bounds.width/5
        let h = self.bounds.height
        let frame = CGRect(x: 0, y: 0, width: w, height: h)
        
        //将tabbar中的第2个位置留出来，给composebtn
        var index = 0
        for v in subviews  {
            //判断当前的v的类型 ???
            if v is UIControl && !(v is UIButton){
                //设置每个子btn的frame 在x上的位移
                v.frame = CGRectOffset(frame, CGFloat(index)*w , 0)
            
                //如果下一个位置是第二个就让index在加1
                if index == 1 {
                    index++
                }
                index++
            }
        }
        
        //设置composeBtn的frame
        composeBtn.frame = CGRectOffset(frame, CGFloat(2)*w, 0)
        
    }
    
    //懒加载btn
    
    lazy var composeBtn:UIButton = {
        var btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_home-1"), forState: UIControlState.Selected)
        
        self.addSubview(btn)
        
        return btn
        
        }()

}
